﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Qizx.Api;

namespace System.Qizx.Xml {

    public class XmlNSManager : XmlNamespaceManager {
        public XmlNSManager(XmlNameTable nameTable)
            : base(nameTable) {
        }

        XmlElement _focus;
        public XmlNSManager(XmlElement focus)
            : base(new NameTable()) {
            _focus = focus;
        }

        public XmlNSManager(XmlElement focus, XmlNameTable nameTable)
            : base(nameTable) {
            _focus = focus;
        }

        public XmlElement FocusNode {
            get { return _focus; }
            set {
                _focus = value;
            }
        }

        public override string LookupNamespace(string prefix) {
            if (_focus != null) {
                string uri = _focus.GetNamespaceOfPrefix(prefix);
                if (uri != null) return uri;
            }

            return base.LookupNamespace(prefix);
        }

        public override string LookupPrefix(string uri) {
            if (_focus != null) {
                string prefix = _focus.GetPrefixOfNamespace(uri);
                if (prefix != null) return prefix;
            }

            return base.LookupNamespace(uri);
        }
    }
}
